package com.ydsungan.controller;


import com.ydsungan.pojo.User;
import com.ydsungan.pojo.UserExample;
import com.ydsungan.service.UserService;
import io.swagger.annotations.ApiImplicitParam;
import org.json.JSONObject;
import com.ydsungan.common.JSONResult;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping(path = "/user")
@Api(value = "用户相关",tags = {"与用户相关的接口"})
@CrossOrigin
public class UserController extends BasicController{

    @Value("${appid}")
    private String appid;

    @Value("${appsecret}")
    private String appsecret;

    @Value("${wxurl}")
    private String wxurl;


    @Autowired
    private UserService userService;

    @Autowired
    private RestTemplate restTemplate;

    @ApiOperation(value="用户登录",notes = "小程序wx.login()调用")
    @ApiImplicitParam(name = "jsCode",value="小程序上传的js code",required = true,dataType = "String")
    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public JSONResult login(String jsCode) throws Exception{
        String url = wxurl + "?appid=" + appid + "&secret=" + appsecret + "&js_code=" + jsCode + "&grant_type=authorization_code";

        ResponseEntity<String> results = restTemplate.exchange(url, HttpMethod.GET, null, String.class);
        String jsonRes = results.getBody();
        JSONObject jsonObj = new JSONObject(jsonRes);
        String openid = (String)jsonObj.get("openid");
        String session_key = (String)jsonObj.get("session_key");
        User user = userService.selectById(openid);
        JSONObject userInfo = new JSONObject();
        if(user == null){
            userInfo.put("openid",openid);
            userInfo.put("session_key",session_key);
            userInfo.put("score",500);
            userInfo.put("newUser",true);
        }else{
            userInfo.put("openid",openid);
            userInfo.put("session_key",session_key);
            userInfo.put("score",user.getScore());
            userInfo.put("newUser",false);
        }
        return JSONResult.ok(userInfo.toString());
    }

    @ApiOperation(value="保存新用户",notes = "新用户需要保存基本信息到数据库")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public JSONResult save(@RequestBody User user) throws Exception{

        user.setScore(500);
        user.setAuth("");
        user.setDescription("");
        userService.saveUser(user);
        return JSONResult.ok();
    }

    @ApiOperation(value="获取所有用户",notes = "获取用户相关信息")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public JSONResult getUsers() throws Exception{


        return JSONResult.ok(userService.getAllUser());
    }



    @ApiOperation(value="获取用户信息",notes = "根据id获取用户信息")
    @RequestMapping(value = "/getuser", method = RequestMethod.GET)
    public JSONResult select(String openId) throws Exception{


        return JSONResult.ok(userService.selectById(openId));
    }

    @ApiOperation(value="更新用户",notes = "用户基本信息需要更新到数据库")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public JSONResult update(@RequestBody User user) throws Exception{
        userService.updateUser(user);
        return JSONResult.ok();
    }

    @ApiOperation(value="更新用户",notes = "选择不为空的更新")
    @RequestMapping(value = "/updateselective", method = RequestMethod.GET)
    public JSONResult updateSelective(String openId, String score) throws Exception{
        User user = new User();
        user.setId(openId);
        user.setScore(Integer.parseInt(score));
        userService.updateUser(user);
        return JSONResult.ok();
    }



}
